package com.tensquare.notice.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.notice.dao.NoticeDao;
import com.tensquare.notice.dao.NoticeFreshDao;
import com.tensquare.notice.feign.ArticleClient;
import com.tensquare.notice.feign.UserClient;
import com.tensquare.notice.pojo.Notice;
import com.tensquare.notice.pojo.NoticeFresh;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoticeService {

    @Autowired(required = false)
    NoticeDao noticeDao;
    @Autowired(required = false)
    NoticeFreshDao noticeFreshDao;
    @Autowired
    IdWorker idWorker;
    @Autowired(required = false)
    ArticleClient articleClient;
    @Autowired(required = false)
    UserClient userClient;

    //1. 根据id查询消息通知
    //2. 根据条件分页查询消息通知
    //3. 新增通知
    //4. 修改通知
    //5. 根据用户id查询该用户的待推送消息（新消息）
    //6. 删除待推送消息（新消息）

    //完善消息对象方法
    private Notice findByNotice(String id){
        Notice notice = noticeDao.selectById(id);
        //查询用户名称
        Result result = userClient.selectById(notice.getOperatorId());
        HashMap data = (HashMap) result.getData();
        //获取操作人名称
        String nickname = (String) data.get("nickname");
        notice.setOperatorName(nickname);

        //查询操作内容名称
        Result byId = articleClient.findById(notice.getTargetId());
        HashMap data1 = (HashMap) byId.getData();
        //获取文章名称
        String title = (String) data1.get("title");
        notice.setTargetName(title);
        return notice;
    }

    //通过id查询消息
    public Notice findById(String id){
      return findByNotice(id);
    }
    //分页查询消息
    public List<Notice> findByPage(int page,int size,Notice notice){
        Map<String, Object> map = JSON.parseObject(JSON.toJSONString(notice));        //创建查询构造器
        EntityWrapper<Notice> employeeEntityWrapper = new EntityWrapper<Notice>();
        //设置查询条件
        employeeEntityWrapper.allEq(map);
        //返回数据
        List<Notice> notices = noticeDao.selectPage(new Page<Notice>(page, size), employeeEntityWrapper);
        return notices;
    }

    //根据userId分页查询待发送消息
    public List<NoticeFresh> findByPageFresh(String uid,String page,String size){
        List<NoticeFresh> employees = noticeFreshDao.selectPage(new Page<NoticeFresh>(1,3),
                new EntityWrapper<NoticeFresh>()
                        .eq("userId",uid)
        );
        return employees;
    }

    //新增通知
    public void add(Notice notice){
        //新增消息信息
        //生成分布式id
        String l = idWorker.nextId()+" ";
        notice.setId(l);
        noticeDao.insert(notice);

        //新增待推送消息
        NoticeFresh noticeFresh = new NoticeFresh();
        noticeFresh.setNoticeId(l);//消息id
        noticeFresh.setUserId(notice.getReceiverId());//待通知用户的id
        noticeFreshDao.insert(noticeFresh);
    }

    //修改通知
    public void save(String id,Notice notice){
        notice.setId(id);
        noticeDao.updateById(notice);
    }

    //根据id查询待推送消息
    public NoticeFresh findByIdFresh(String id)
    {
       return noticeFreshDao.selectById(id);
    }
    //删除待推送消息
    public void deleteFresh(String id){
        noticeFreshDao.deleteById(id);
    }
}
